package com.lb.shopguide.adapter;

import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chauthai.swipereveallayout.SwipeRevealLayout;
import com.lb.shopguide.R;
import com.lb.shopguide.entity.order.GoodsInOrderBean;
import com.lb.shopguide.ui.view.LbGoodsNumSelector;
import com.lb.shopguide.util.pic.ImageLoaderUtils;
import com.yqritc.recyclerviewflexibledivider.FlexibleDividerDecoration;

import java.util.List;

/**
 * Created by kang on 2017/6/1.
 * 收银台
 */

public class AdapterGoodsInChangePrice extends BaseQuickAdapter<GoodsInOrderBean, BaseViewHolder> implements FlexibleDividerDecoration.PaintProvider {

    public AdapterGoodsInChangePrice(int layoutResId, List<GoodsInOrderBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(final BaseViewHolder helper, final GoodsInOrderBean item) {
        View view = helper.itemView;
        SwipeRevealLayout revealLayout = view.findViewById(R.id.swipe_reveal_layout);
        TextView tvProductName = view.findViewById(R.id.tv_product_name);
        TextView tvSize = view.findViewById(R.id.tv_size);
        TextView tvPrice = view.findViewById(R.id.tv_price);
        TextView tvSum = view.findViewById(R.id.tv_sum);
        TextView tvOriginPrice = view.findViewById(R.id.tv_price_origin);
        EditText etDiscount = view.findViewById(R.id.et_money);
        ImageView ivProductPic = view.findViewById(R.id.iv_product);
        RelativeLayout layoutDelete = view.findViewById(R.id.delete_layout);
        LbGoodsNumSelector numSelector = view.findViewById(R.id.selector_goods_num);
        RelativeLayout layoutDiscount = view.findViewById(R.id.layout_discount);
        CheckBox checkBox = view.findViewById(R.id.checkbox);
        tvOriginPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
        //编辑模式
        tvSum.setVisibility(View.GONE);
        numSelector.setVisibility(View.GONE);
        layoutDiscount.setVisibility(View.VISIBLE);
        revealLayout.setLockDrag(false);
        checkBox.setVisibility(View.GONE);
        etDiscount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (mOnItemEditTextChangedListener != null) {
                    mOnItemEditTextChangedListener.onEditTextAfterTextChanged(s, helper.getLayoutPosition());
                }
            }
        });
        if (item.getDiscountPrice() != item.getPrice()) {
            tvOriginPrice.setVisibility(View.VISIBLE);
            tvOriginPrice.setText("￥" + item.getPrice());
            tvPrice.setText("￥" + item.getPrice());
            etDiscount.setText("" + item.getDiscountPrice());
        } else {
            tvOriginPrice.setVisibility(View.GONE);
            tvPrice.setText("￥" + item.getPrice());
            etDiscount.setText("" + item.getDiscountPrice());
        }

        ImageLoaderUtils.getGlideManager().asBitmap().load(item.getProductPicUrl()).apply(ImageLoaderUtils.getSGCommonGlideOption(8)).into(ivProductPic);
        tvSize.setText(String.format(mContext.getResources().getString(R.string.format_string_size), item.getColor(), item.getSize()));
        tvProductName.setText(item.getProductName());
        tvSum.setText("x" + item.getProductSum());
        numSelector.setInitNumber("" + item.getProductSum());
    }

    private OnItemEditTextChangedListener mOnItemEditTextChangedListener;

    public interface OnItemEditTextChangedListener {
        void onEditTextAfterTextChanged(Editable editable, int position);
    }

    public void setOnItemEditTextChangedListener(OnItemEditTextChangedListener listener) {
        mOnItemEditTextChangedListener = listener;
    }

    @Override
    public Paint dividerPaint(int position, RecyclerView parent) {
        Paint paint = new Paint();
        paint.setColor(mContext.getResources().getColor(R.color.sg_common_line_bg));
        paint.setStrokeWidth(10);
        return paint;
    }
}
